//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ksyun.media.shortvideo.kit;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import com.ksyun.media.player.IMediaPlayer;
import com.ksyun.media.player.KSYMediaMeta;
import com.ksyun.media.player.KSYMediaPlayer;
import com.ksyun.media.player.IMediaPlayer.OnCompletionListener;
import com.ksyun.media.player.IMediaPlayer.OnPreparedListener;
import com.ksyun.media.player.misc.KSYProbeMediaInfo;
import com.ksyun.media.shortvideo.a.c;
import com.ksyun.media.shortvideo.sticker.KSYStickerView;
import com.ksyun.media.shortvideo.utils.FileUtils;
import com.ksyun.media.shortvideo.utils.KS3ClientWrap.KS3UploadInfo;
import com.ksyun.media.streamer.capture.AudioPlayerCapture;
import com.ksyun.media.streamer.capture.WaterMarkCapture;
import com.ksyun.media.streamer.capture.AudioPlayerCapture.PlayRanges;
import com.ksyun.media.streamer.demuxer.AVDemuxerCapture.OnVideoPtsChangedListener;
import com.ksyun.media.streamer.encoder.AudioEncodeFormat;
import com.ksyun.media.streamer.filter.audio.AudioFilterBase;
import com.ksyun.media.streamer.filter.audio.AudioFilterMgt;
import com.ksyun.media.streamer.filter.audio.AudioPreview;
import com.ksyun.media.streamer.filter.audio.AudioReverbFilter;
import com.ksyun.media.streamer.filter.audio.KSYAudioEffectFilter;
import com.ksyun.media.streamer.filter.imgtex.ImgBeautyProFilter;
import com.ksyun.media.streamer.filter.imgtex.ImgBeautySmoothFilter;
import com.ksyun.media.streamer.filter.imgtex.ImgBeautyStylizeFilter;
import com.ksyun.media.streamer.filter.imgtex.ImgFilterBase;
import com.ksyun.media.streamer.filter.imgtex.ImgTexFilter;
import com.ksyun.media.streamer.filter.imgtex.ImgTexFilterMgt;
import com.ksyun.media.streamer.filter.imgtex.ImgTexMixer;
import com.ksyun.media.streamer.filter.imgtex.ImgTexPreview;
import com.ksyun.media.streamer.util.gles.GLRender;
import com.ksyun.media.streamer.util.gles.GLRender.GLRenderListener;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
/**
 * 视频处理类
 * Rookie
 * 1939428911@qq.com
 * 2019/2/26 15:30
 */
public class MyKSYEditKit {
    private static final String a = "KSYEditKit";
    private static final boolean b = false;
    private Context context;
    private static final int d = 2;
    public static final int SCALING_MODE_BEST_FIT = 1;
    public static final int SCALING_MODE_CROP = 3;
    public static final int SCALING_MODE_FULL_FILL = 0;
    public static int SCALE_TYPE_9_16 = 1;
    public static int SCALE_TYPE_3_4 = 2;
    public static int SCALE_TYPE_1_1 = 3;
    public static float DEFAULT_SCALE = 0.5625F;
    private int e = 0;
    private int f = 1;
    private String g;
    private int h = 0;
    private int i = 0;
    private int j = 0;
    private int k = 0;
    private GLRender l;
    private WaterMarkCapture m;
    private ImgTexMixer n;
    private ImgTexFilterMgt o;
    private com.ksyun.media.shortvideo.kit.b p;
    private ImgTexPreview q;
    private AudioFilterMgt r;
    private AudioPlayerCapture s;
    private AudioPreview t;
    private Handler u;
    private boolean v = false;
    private boolean w = false;
    private long x = 0L;
    private float y = 1.0F;
    private MyKSYEditKit.OnInfoListener z;
    private MyKSYEditKit.OnErrorListener A;
    private com.ksyun.media.shortvideo.kit.a B;
    private String C = "/sdcard/dest.mp4";
    private String D = "/sdcard/dest.mp4";
    private List<ImgFilterBase> F;
    private List<ImgFilterBase> G;
    private List<AudioFilterBase> H;
    private KSYStickerView I;
    private Timer J;
    private TimerTask K;
    private View L;
    private String M = null;
    private MyKSYEditKit.b N = null;
    private long O;
    private long P;
    private com.ksyun.media.shortvideo.a.b.a Q;
    private c R;
    private String S;
    private String T;
    public com.ksyun.media.player.IMediaPlayer.OnInfoListener mMediaPlayerOnInfoListener = new com.ksyun.media.player.IMediaPlayer.OnInfoListener() {
        public boolean onInfo(IMediaPlayer var1, int var2, int var3) {
            Log.d("KSYEditKit", "Media Player info:" + var2 + "_" + var3);
            MyKSYEditKit.this.a(6, String.valueOf(var2));
            return false;
        }
    };
    public com.ksyun.media.player.IMediaPlayer.OnErrorListener mMediaPlayerOnErrorListener = new com.ksyun.media.player.IMediaPlayer.OnErrorListener() {
        public boolean onError(IMediaPlayer var1, int var2, int var3) {
            Log.d("KSYEditKit", "Media Player error:" + var2 + "_" + var3);
            MyKSYEditKit.this.a(-4, (long)var2);
            return false;
        }
    };
    public OnPreparedListener mOnPreparedListener = new OnPreparedListener() {
        public void onPrepared(IMediaPlayer var1) {
            KSYMediaPlayer var2 = (KSYMediaPlayer)var1;
            KSYMediaMeta var3 = var2.getMediaInfo().mMeta;
            if(MyKSYEditKit.this.x == 0L) {
                MyKSYEditKit.this.x = var2.getDuration();
                if(MyKSYEditKit.this.z != null) {
                    MyKSYEditKit.this.z.onInfo(1, new String[0]);
                }
            }

            boolean var4 = false;
            boolean var5 = false;
            boolean var6 = false;
            boolean var7 = false;
            if(var3.mVideoStream == null) {
                Log.d("KSYEditKit", "audio only");
                var7 = true;
            } else {
                int var9 = var2.getVideoWidth();
                int var10 = var2.getVideoHeight();
                if(MyKSYEditKit.this.p.h() % 180 != 0) {
                    int var8 = var9;
                    var9 = var10;
                    var10 = var8;
                }

                MyKSYEditKit.this.j = var9;
                MyKSYEditKit.this.k = var10;
            }

            MyKSYEditKit.this.m.setPreviewSize(MyKSYEditKit.this.j, MyKSYEditKit.this.k);
            if(var3.mAudioStream == null) {
                var6 = true;
                Log.d("KSYEditKit", "video only");
            }

        }
    };

    private com.ksyun.media.shortvideo.kit.a.b V = new com.ksyun.media.shortvideo.kit.a.b() {
        public void onInfo(com.ksyun.media.shortvideo.kit.a var1, int var2, String var3) {
            Log.d("KSYEditKit", "compose info:" + var2);
            switch(var2) {
            case 1:
                MyKSYEditKit.this.a(2, var3);
                break;
            case 2:
                MyKSYEditKit.this.a(false, 0);
                MyKSYEditKit.this.B.getAVDemuxerCapture().setOnVideoPtsChangedListener((OnVideoPtsChangedListener)null);
                if((!TextUtils.isEmpty(MyKSYEditKit.this.T) || !TextUtils.isEmpty(MyKSYEditKit.this.S)) && !FileUtils.getMimeType(new File(var3)).equals(FileUtils.MIME_TYPE_GIF)) {
                    MyKSYEditKit.this.a(var3);
                } else {
                    MyKSYEditKit.this.a(3, var3);
                }
                break;
            case 3:
                MyKSYEditKit.this.a(4, var3);
                break;
            default:
                MyKSYEditKit.this.a(var2, var3);
            }

        }
    };
    private com.ksyun.media.shortvideo.kit.a.a W = new com.ksyun.media.shortvideo.kit.a.a() {
        public void onError(com.ksyun.media.shortvideo.kit.a var1, int var2, long var3) {
            MyKSYEditKit.this.a(true, var2);
            MyKSYEditKit.this.a(var2, var3);
        }
    };
    public GLRenderListener mGLRenderListener = new GLRenderListener() {
        public void onReady() {
        }

        public void onSizeChanged(int var1, int var2) {
            MyKSYEditKit.this.h = var1;
            MyKSYEditKit.this.i = var2;
            Log.d("KSYEditKit", "screenWidth * screenHeight:" + MyKSYEditKit.this.h + "*" + MyKSYEditKit.this.i);
            MyKSYEditKit.this.e();
            if(MyKSYEditKit.this.N != null) {
                MyKSYEditKit.this.showWaterMarkLogo(MyKSYEditKit.this.N.a, MyKSYEditKit.this.N.b, MyKSYEditKit.this.N.c, MyKSYEditKit.this.N.d, MyKSYEditKit.this.N.e, MyKSYEditKit.this.N.f);
            }

            if(MyKSYEditKit.this.v) {
                MyKSYEditKit.this.v = false;
                MyKSYEditKit.this.startEditPreview();
            }

            if(!MyKSYEditKit.this.p.a().isPlaying()) {
                MyKSYEditKit.this.p.i();
            }

        }

        public void onDrawFrame() {
        }

        public void onReleased() {
        }
    };

    public MyKSYEditKit(Context var1) {
        if(var1 == null) {
            throw new IllegalArgumentException("Context cannot be null!");
        } else {
            this.context = var1.getApplicationContext();
            this.u = new MyKSYEditKit.a(this, Looper.getMainLooper());
             c.a().a(this.context);
            this.a();
        }
    }

    private void a() {
        this.l = new GLRender();
        this.m = new WaterMarkCapture(this.l);
        this.p = new com.ksyun.media.shortvideo.kit.b(this.context, this.l);
        this.p.a(true);
        this.o = new ImgTexFilterMgt(this.context);
        this.n = new ImgTexMixer(this.l);
        this.q = new ImgTexPreview();
        this.n.setScalingMode(this.e, 1);
        this.p.c.connect(this.o.getSinkPin());
        this.o.getSrcPin().connect(this.n.getSinkPin(this.e));
        this.m.mLogoTexSrcPin.connect(this.n.getSinkPin(this.f));
        this.n.getSrcPin().connect(this.q.getSinkPin());
        this.s = new AudioPlayerCapture(this.context);
        this.r = new AudioFilterMgt();
        this.t = new AudioPreview(this.context);
        this.p.a.connect(this.r.getSinkPin());
        this.r.getSrcPin().connect(this.t.getSinkPin());
        this.t.setBlockingMode(true);
        this.B = new com.ksyun.media.shortvideo.kit.a(this.context);
        this.B.setOnInfoListener(this.V);
        this.B.setOnErrorListener(this.W);
        this.F = new LinkedList();
        this.G = new LinkedList();
        this.H = new LinkedList();
        this.p.a(this.mMediaPlayerOnInfoListener);
        this.p.a(this.mOnPreparedListener);
        this.p.a(this.mMediaPlayerOnErrorListener);
        this.p.a(new OnCompletionListener() {
            public void onCompletion(IMediaPlayer var1) {
                if(MyKSYEditKit.this.s.getMediaPlayer().isPlaying()) {
                    MyKSYEditKit.this.s.restart();
                }

            }
        });
        this.l.addListener(new GLRenderListener() {
            public void onReady() {
                MyKSYEditKit.this.q.setEGL10Context(MyKSYEditKit.this.l.getEGL10Context());
            }

            public void onSizeChanged(int var1, int var2) {
            }

            public void onDrawFrame() {
            }

            public void onReleased() {
            }
        });
        this.l.init(1, 1);
        this.Q = new com.ksyun.media.shortvideo.a.b.a();
        this.R = c.a();
    }

    public GLRender getGLRender() {
        return this.l;
    }

    public boolean getIsLandscape() {
        return this.j >= this.k;
    }

    public ImgTexFilterMgt getImgTexFilterMgt() {
        return this.o;
    }

    public AudioFilterMgt getAudioFilterMgt() {
        return this.r;
    }

    public ImgTexMixer getImgTexPreviewMixer() {
        return this.n;
    }

    public AudioPlayerCapture getAudioPlayerCapture() {
        return this.s;
    }

    public void setDisplayPreview(GLSurfaceView var1) {
        this.q.setDisplayPreview(var1);
        this.q.getGLRender().addListener(this.mGLRenderListener);
    }

    public void setDisplayPreview(TextureView var1) {
        this.q.setDisplayPreview(var1);
        this.q.getGLRender().addListener(this.mGLRenderListener);
    }

    public void setSpeed(float var1) {
        this.p.a(var1);
    }

    public float getSpeed() {
        return this.p.b();
    }

    public float getMAXSpeed() {
        return 2.0F;
    }

    public float getNomalSpeed() {
        return 1.0F;
    }

    public float getMINSpeed() {
        return 0.5F;
    }

    public void updateSpeed(boolean var1) {
        float var2 = this.p.b();
        if(var1) {
            var2 += 0.1F;
            if((double)var2 > 2.0D) {
                var2 = 2.0F;
            }

            this.p.a(var2);
        } else {
            var2 = (float)((double)var2 - 0.1D);
            if((double)var2 < 0.5D) {
                var2 = 0.5F;
            }

            this.p.a(var2);
        }

    }

    public void setEditPreviewUrl(String var1) {
        if(TextUtils.isEmpty(var1)) {
            throw new IllegalArgumentException("url can not be null");
        } else if(!FileUtils.isSupportedFile(var1)) {
            throw new IllegalArgumentException("the file must be mp4 or 3gpp or mov");
        } else {
            this.g = var1;
        }
    }

    public void setPreviewCrop(float var1, float var2, float var3, float var4) {
        if(this.getCropScale() != 0.0F) {
            float var5 = this.getCropScale();
            if(!this.getIsLandscape()) {
                var3 = 1.0F;
                var4 = 1.0F - var5;
            } else {
                var3 = 1.0F - var5;
                var4 = 1.0F;
            }

            this.n.setRectForCrop(0, var1, var2, var3, var4);
            this.B.getImgTexMixer().setRectForCrop(0, var1, var2, var3, var4);
            if(!this.p.a().isPlaying()) {
                this.p.i();
            }

        }
    }

    public RectF getPreviewCropRect() {
        return this.n.getRectForCrop(0);
    }

    public void setScalingMode(int var1) {
        this.n.setScalingMode(this.e, var1);
        this.B.setScalingMode(var1);
        if(this.N != null) {
            this.m.hideLogo();
            this.B.hideWaterMarkLogo();
            this.showWaterMarkLogo(this.N.a, this.N.b, this.N.c, this.N.d, this.N.e, this.N.f);
        }

    }

    public void setScaleType(int var1) {
        float var2 = DEFAULT_SCALE;
        if(var1 == SCALE_TYPE_9_16) {
            var2 = 0.5625F;
        } else if(var1 == SCALE_TYPE_3_4) {
            var2 = 0.75F;
        } else if(var1 == SCALE_TYPE_1_1) {
            var2 = 1.0F;
        }

        if(var2 != this.B.getScale()) {
            this.B.setScale(var2);
            if(this.N != null) {
                this.m.hideLogo();
                this.B.hideWaterMarkLogo();
            }

            if(this.B.getScaleMode() == 3) {
                this.n.setRectForCrop(0, 0.0F, 0.0F, 1.0F, 1.0F);
                this.B.getImgTexMixer().setRectForCrop(0, 0.0F, 0.0F, 1.0F, 1.0F);
            }
        }

    }

    public void setScale(float var1) {
        if(var1 != this.B.getScale()) {
            this.B.setScale(var1);
            this.m.hideLogo();
            this.B.hideWaterMarkLogo();
            if(this.B.getScaleMode() == 3) {
                this.n.setRectForCrop(0, 0.0F, 0.0F, 1.0F, 1.0F);
                this.B.getImgTexMixer().setRectForCrop(0, 0.0F, 0.0F, 1.0F, 1.0F);
            }
        }

    }

    public float getCropScale() {
        float var1 = (float)this.j / (float)this.k;
        float var2 = (float)this.h / (float)this.i;
        float var3;
        if(var1 > var2) {
            var3 = 1.0F - var2 / var1;
        } else {
            var3 = 1.0F - var1 / var2;
        }

        return var3;
    }

    public String getEditUrl() {
        return this.g;
    }

    public void startEditPreview() {
        if(TextUtils.isEmpty(this.g)) {
            throw new IllegalArgumentException("url can not be null");
        } else if(!FileUtils.isSupportedFile(this.g)) {
            throw new IllegalArgumentException("the file must be mp4 or 3gpp or mov");
        } else {
            if(this.h != 0 && this.i != 0) {
                this.e();
                this.p.b(false);
                this.p.a(1);
                this.p.a(this.g);
                this.t.start();
            } else {
                this.v = true;
            }

            if(this.I != null) {
                this.I.stopPreview(false);
            }

        }
    }

    public long getEditDuration() {
        return this.x;
    }

    public String getVideoCodecMeta() {
        if(this.getMediaPlayer() != null) {
            Bundle var1 = this.getMediaPlayer().getMediaMeta();
            KSYMediaMeta var2 = KSYMediaMeta.parse(var1);
            return var2.getVideoCodec();
        } else {
            return null;
        }
    }

    public void seekEditPreview(long var1) {
        if(this.p.a() != null && this.p.a().isPlaying()) {
            com.ksyun.media.shortvideo.kit.b.b var3 = this.p.c();
            if(var3 != null) {
                if(var1 < var3.startTime) {
                    var1 = var3.startTime;
                }

                if(var1 > var3.endTime) {
                    var1 = var3.endTime;
                }
            }

            if(var1 > this.x) {
                var1 = this.x;
            }

            if(var1 > this.x) {
                var1 = this.x;
            }

            if(var1 < 0L) {
                var1 = 0L;
            }

            this.p.a().seekTo(var1);
        }

    }

    public long getEditPreviewCurrentPosition() {
        return this.p.a().getCurrentPosition();
    }

    public Bitmap getVideoThumbnailAtTime(long var1, int var3, int var4, boolean var5) {
        KSYProbeMediaInfo var6 = new KSYProbeMediaInfo();
        return var6.getVideoThumbnailAtTime(this.g, var1, var3, var4, var5);
    }

    public void setEditPreviewRanges(long var1, long var3, boolean var5) {
        if(var1 >= 0L && var3 >= var1) {
            if(var5) {
                this.p.a(var1, var3);
                this.B.setComposeRanges(var1, var3);
                this.p.f();
            } else {
                this.p.a(var1, var3);
                this.B.setComposeRanges(var1, var3);
            }
        }

    }

    public void stopEditPreview() {
        if(this.I != null) {
            this.I.stopPreview(true);
        }

        this.s.stop();
        this.p.e();
        this.t.stop();
    }

    public void startCompose(String var1) {
        this.C = var1;

        Log.d("KSYEditKit", "auth success start compose:" + MyKSYEditKit.this.C);
        MyKSYEditKit.this.B.setScreenRenderSize(MyKSYEditKit.this.h, MyKSYEditKit.this.i);
        MyKSYEditKit.this.B.addPaint(MyKSYEditKit.this.L);
        MyKSYEditKit.this.c();
        MyKSYEditKit.this.d();
        MyKSYEditKit.this.b();
        MyKSYEditKit.this.B.setSpeed(MyKSYEditKit.this.p.b());
        MyKSYEditKit.this.B.setSrcUrl(MyKSYEditKit.this.g);
        File var2 = new File(MyKSYEditKit.this.C);
        if(TextUtils.isEmpty(MyKSYEditKit.this.T) && TextUtils.isEmpty(MyKSYEditKit.this.S) || !FileUtils.getMimeType(var2).equals(FileUtils.MIME_TYPE_MP4) && !FileUtils.getMimeType(var2).equals(FileUtils.MIME_TYPE_EXT_MP4)) {
            MyKSYEditKit.this.B.setDesUrl(MyKSYEditKit.this.C);
        } else {
            MyKSYEditKit.this.D = MyKSYEditKit.this.getTmpComposePath(MyKSYEditKit.this.C);
            MyKSYEditKit.this.B.setDesUrl(MyKSYEditKit.this.D);
        }
        MyKSYEditKit.this.g();
        MyKSYEditKit.this.O = System.currentTimeMillis();
        MyKSYEditKit.this.B.start();

    }

    private void b() {
        this.Q.k = 0;
        this.Q.j = 0;
        this.B.getAudioFilterMgt().setFilter((AudioFilterBase)null);
        this.H.clear();
        List var1 = this.r.getFilter();
        if(var1 != null) {
            for(int var2 = 0; var2 < var1.size(); ++var2) {
                if(var1.get(var2) instanceof KSYAudioEffectFilter) {
                    int var3 = ((KSYAudioEffectFilter)var1.get(var2)).getAudioEffectType();
                    KSYAudioEffectFilter var4 = new KSYAudioEffectFilter(var3);
                    if(var3 == KSYAudioEffectFilter.AUDIO_EFFECT_TYPE_PITCH) {
                        var4.setPitchLevel(var4.getPitchLevel());
                    }

                    this.H.add(var4);
                    this.Q.k = 1;
                } else if(var1.get(var2) instanceof AudioReverbFilter) {
                    AudioReverbFilter var5 = new AudioReverbFilter();
                    var5.setReverbLevel(((AudioReverbFilter)var1.get(var2)).getReverbType());
                    this.H.add(var5);
                    this.Q.j = 1;
                } else {
                    this.H.add((AudioFilterBase) var1.get(var2));
                }
            }

            this.B.getAudioFilterMgt().setFilter(this.H);
        }

    }

    private void c() {
        this.B.getImgTexFilterMgt().setFilter((ImgFilterBase)null);
        this.B.getImgTexFilterMgt().setExtraFilter((ImgFilterBase)null);
        this.F.clear();
        this.G.clear();
        List var1 = this.o.getFilter();
        List var2 = this.o.getExtraFilters();
        ImgBeautyProFilter var3 = null;
        ImgBeautySmoothFilter var4 = null;
        ImgBeautyProFilter var5 = null;
        ImgBeautySmoothFilter var6 = null;
        ImgBeautyStylizeFilter var7 = null;
        int var8;
        if(var1 != null) {
            for(var8 = 0; var8 < var1.size(); ++var8) {
                if(var1.get(var8) instanceof ImgBeautyProFilter) {
                    var3 = (ImgBeautyProFilter)var1.get(var8);
                } else if(var1.get(var8) instanceof ImgBeautySmoothFilter) {
                    var4 = (ImgBeautySmoothFilter)var1.get(var8);
                } else if(var1.get(var8) instanceof ImgBeautyStylizeFilter) {
                    var7 = (ImgBeautyStylizeFilter)var1.get(var8);
                } else {
                    this.F.add((ImgFilterBase) var1.get(var8));
                }
            }
        }

        if(var2 != null) {
            for(var8 = 0; var8 < var2.size(); ++var8) {
                if(var2.get(var8) instanceof ImgBeautyProFilter) {
                    var5 = (ImgBeautyProFilter)var2.get(var8);
                } else if(var2.get(var8) instanceof ImgBeautySmoothFilter) {
                    var6 = (ImgBeautySmoothFilter)var2.get(var8);
                } else {
                    this.G.add((ImgFilterBase) var2.get(var8));
                }
            }
        }

        this.o.setFilter((ImgFilterBase)null);
        this.o.setExtraFilter((ImgFilterBase)null);
        ImgTexFilter var9;
        if(this.F != null) {
            for(var8 = 0; var8 < this.F.size(); ++var8) {
                if(this.F.get(var8) instanceof ImgTexFilter) {
                    var9 = (ImgTexFilter)this.F.get(var8);
                    var9.setGLRender(this.B.getGLRender());
                }
            }
        }

        if(this.G != null) {
            for(var8 = 0; var8 < this.G.size(); ++var8) {
                if(this.G.get(var8) instanceof ImgTexFilter) {
                    var9 = (ImgTexFilter)this.G.get(var8);
                    var9.setGLRender(this.B.getGLRender());
                }
            }
        }

        ImgBeautyProFilter var10;
        if(var3 != null) {
            var10 = new ImgBeautyProFilter(this.B.getGLRender(), this.context);
            var10.setGrindRatio(var3.getGrindRatio());
            var10.setRuddyRatio(var3.getRuddyRatio());
            var10.setWhitenRatio(var3.getWhitenRatio());
            this.F.add(var10);
        }

        ImgBeautySmoothFilter var11;
        if(var4 != null) {
            var11 = new ImgBeautySmoothFilter(this.B.getGLRender(), this.context);
            var11.setWhitenRatio(var4.getWhitenRatio());
            var11.setRuddyRatio(var4.getRuddyRatio());
            var11.setGrindRatio(var4.getGrindRatio());
            this.F.add(var11);
        }

        if(var5 != null) {
            var10 = new ImgBeautyProFilter(this.B.getGLRender(), this.context);
            var10.setGrindRatio(var5.getGrindRatio());
            var10.setRuddyRatio(var5.getRuddyRatio());
            var10.setWhitenRatio(var5.getWhitenRatio());
            this.G.add(var10);
        }

        if(var6 != null) {
            var11 = new ImgBeautySmoothFilter(this.B.getGLRender());
            var11.setWhitenRatio(var6.getWhitenRatio());
            var11.setRuddyRatio(var6.getRuddyRatio());
            var11.setGrindRatio(var6.getGrindRatio());
            this.G.add(var11);
        }

        if(var7 != null) {
            ImgBeautyStylizeFilter var12 = new ImgBeautyStylizeFilter(this.B.getGLRender(), this.context, var7.getStyleFilterId());
            this.F.add(var12);
        }

        this.B.getImgTexFilterMgt().setFilter(this.F);
        this.B.getImgTexFilterMgt().setExtraFilter(this.G);
        if(this.F != null && this.F.size() > 0 || this.G != null && this.G.size() > 0) {
            this.Q.i = 1;
        } else {
            this.Q.i = 0;
        }

    }

    private void d() {
        if(this.I != null) {
            this.I.configSticker();
            final TreeMap var1 = this.I.getStickerEvents();
            if(var1 != null && var1.size() > 0) {
                this.B.getAVDemuxerCapture().setOnVideoPtsChangedListener(new OnVideoPtsChangedListener() {
                    public void onVideoPtsChanged(long var1x) {
                        if(var1 != null && var1.size() > 0) {
                            MyKSYEditKit.this.a(var1, var1x);
                        }

                    }
                });
                if((this.I.getStickerUsingState() & 1) == 1) {
                    this.Q.r = 1;
                }

                if((this.I.getStickerUsingState() & 2) == 2) {
                    this.Q.u = 1;
                }
            } else {
                this.B.hideSticker();
                this.Q.r = 0;
                this.Q.u = 0;
            }
        }

    }

    private void a(TreeMap<Long, List<com.ksyun.media.shortvideo.sticker.b>> var1, long var2) {
        if(var1 != null && var1.size() > 0) {
            long var4 = ((Long)var1.firstKey()).longValue();
            if(var4 <= var2) {
                List var6 = (List)var1.get(Long.valueOf(var4));
                LinkedList var7 = new LinkedList();
                boolean var8 = false;
                Iterator var9 = var6.iterator();

                while(var9.hasNext()) {
                    com.ksyun.media.shortvideo.sticker.b var10 = (com.ksyun.media.shortvideo.sticker.b)var9.next();
                    if(var10.a.c() == 4) {
                        var8 = true;
                    }

                    if(var10.c) {
                        var7.add(var10.a);
                    }
                }

                com.ksyun.media.shortvideo.sticker.c var18 = this.I.saveStickers(var7);
                if(var18 != null) {
                    float var19 = var18.b / (float)this.h;
                    float var11 = var18.c / (float)this.i;
                    float var12 = var18.d / (float)this.h;
                    float var13 = var18.e / (float)this.i;
                    float var14 = var19;
                    float var15 = var11;
                    if(this.B.getScaleMode() == 0 && this.j != 0 && this.h != 0) {
                        float var16 = (float)this.j / (float)this.k;
                        float var17 = (float)this.h / (float)this.i;
                        if(var16 < var17) {
                            var14 = (var18.b * 2.0F - (float)this.h + (float)this.i * var16) / ((float)(2 * this.i) * var16);
                            var12 = 0.0F;
                        } else if(var16 > var17) {
                            var15 = (var18.c * var16 * 2.0F - (float)this.i * var16 + (float)this.h) / (float)(2 * this.h);
                            var13 = 0.0F;
                        }
                    }

                    this.B.showSticker(var18.a, var14, var15, var12, var13, 1.0F);
                } else {
                    this.B.hideSticker();
                }

                if(!var8) {
                    var1.remove(Long.valueOf(var4));
                } else {
                    this.a(var1, var4, var2);
                }
            }
        }

    }

    private void a(TreeMap<Long, List<com.ksyun.media.shortvideo.sticker.b>> var1, long var2, long var4) {
        List var6 = (List)var1.get(Long.valueOf(var2));
        int var7 = 0;
        long var8 = 9223372036854775807L;
        Iterator var10 = var1.keySet().iterator();

        while(var10.hasNext()) {
            long var11 = ((Long)var10.next()).longValue();
            ++var7;
            if(var7 == 2) {
                if(((List)var1.get(Long.valueOf(var11))).size() > 0) {
                    var8 = var11;
                }
                break;
            }
        }

        if(var8 <= var4) {
            var1.remove(Long.valueOf(var2));
        } else {
            var10 = var6.iterator();

            while(var10.hasNext()) {
                com.ksyun.media.shortvideo.sticker.b var13 = (com.ksyun.media.shortvideo.sticker.b)var10.next();
                if(var13.a.k() < var4) {
                    var13.a.e();
                    var10.remove();
                }
            }

            if(var6.size() <= 0) {
                var1.remove(Long.valueOf(var2));
            }

        }
    }

    public int getProgress() {
        return this.B != null?(int)(this.B.getProgress() * 100.0F):0;
    }

    public void stopCompose() {
        if(this.B != null) {
            this.B.stopCompose();
        }

    }


    public void setVideoFps(float var1) {
        this.B.setVideoFps(var1);
    }

    public void setIFrameInterval(float var1) {
        if(var1 <= 0.0F) {
            throw new IllegalArgumentException("I frame interval must be positive");
        } else {
            this.B.setIFrameInterval(var1);
        }
    }

    public void setVideoCrf(int var1) {
        this.B.setVideoCrf(var1);
    }

    public void setVideoBitrate(int var1) {
        this.B.setVideoBitrate(var1);
    }

    public void setVideoKBitrate(int var1) {
        this.setVideoBitrate(var1 * 1000);
    }

    public void setAudioBitrate(int var1) {
        this.B.setAudioBitrate(var1);
    }

    public void setAudioKBitrate(int var1) {
        this.setAudioBitrate(var1 * 1000);
    }

    public void setTargetResolution(int var1, int var2) {
        this.B.setTargetResolution(var1, var2);
    }

    public void setTargetResolution(int var1) {
        this.B.setTargetResolution(var1);
    }

    public void setVideoCodecId(int var1) {
        this.B.setVideoCodecId(var1);
    }

    public void setEncodeMethod(int var1) {
        this.B.setEncodeMethod(var1);
    }

    public void setAudioEncodeMethod(int var1) {
        this.B.setAudioEncodeMethod(var1);
    }

    public void setVideoEncodeMethod(int var1) {
        this.B.setVideoEncodeMethod(var1);
    }

    public void setVideoDecodeMethod(int var1) {
        this.B.setVideoDecodeMethod(var1);
    }

    public void setVideoEncodeProfile(int var1) {
        this.B.setVideoEncodeProfile(var1);
    }

    public void setForceVideoFrameFirst(boolean var1) {
        this.B.setForceVideoFrameFirst(var1);
    }

    public void setOnInfoListener(MyKSYEditKit.OnInfoListener var1) {
        this.z = var1;
    }

    public void setOnErrorListener(MyKSYEditKit.OnErrorListener var1) {
        this.A = var1;
    }

    private void a(final int var1, final long var2) {
        if(this.u != null) {
            this.u.post(new Runnable() {
                public void run() {
                    if(MyKSYEditKit.this.A != null) {
                        MyKSYEditKit.this.A.onError(var1, var2);
                    }

                }
            });
        }

    }

    private void a(final int var1, final String var2) {
        this.u.post(new Runnable() {
            public void run() {
                if(MyKSYEditKit.this.z != null) {
                    String[] var1x = new String[]{var2};
                    Object var2x = MyKSYEditKit.this.z.onInfo(var1, var1x);
                    if(var1 != 2 && var1 == 3) {
                        if(var2x instanceof KS3UploadInfo) {
                            KS3UploadInfo var3 = (KS3UploadInfo)var2x;
                            if(!TextUtils.isEmpty(var3.bucket) && !TextUtils.isEmpty(var3.objectkey)) {
                                MyKSYEditKit.this.a(-3, 0L);
                            } else {
                                MyKSYEditKit.this.a(-2, 0L);
                            }
                        } else {
                            MyKSYEditKit.this.a(-2, 0L);
                        }
                    }
                }

            }
        });
    }

    private void e() {
        this.m.setTargetSize(this.h, this.i);
        this.n.setTargetSize(this.h, this.i);
    }

    public void onResume() {
        Log.d("KSYEditKit", "onResume");
        if(this.I != null) {
            this.I.stopPreview(false);
        }

        this.q.onResume();
        this.t.start();
        if(this.p.a().isPlayable() && !this.p.a().isPlaying() && this.w) {
            this.w = false;
            this.p.a().start();
        }

    }

    public void onPause() {
        Log.d("KSYEditKit", "onPause");
        if(this.I != null) {
            this.I.stopPreview(true);
        }

        this.q.onPause();
        this.t.stop();
        if(this.p.a().isPlaying()) {
            this.w = true;
            this.p.a().pause();
        }

    }

    public void resumeEditPreview() {
        if(this.I != null) {
            this.I.stopPreview(false);
        }

        this.q.onResume();
        this.p.a().start();
        this.s.resume();
        this.t.start();
    }

    public void pauseEditPreview() {
        if(this.I != null) {
            this.I.stopPreview(true);
        }

        this.q.onPause();
        this.p.a().pause();
        this.s.pause();
        this.t.stop();
    }

    public void enableOriginAudio(boolean var1) {
        this.p.d(!var1);
        this.B.enableOriginAudio(var1);
    }

    public void setOriginAudioVolume(float var1) {
        this.y = var1;
        if(this.p.a() != null && this.p.a().isPlaying()) {
            this.p.b(var1);
        }

        this.B.setAudioVolume(this.B.mIdxAudioOrigin, var1);
    }

    public float getOriginAudioVolume() {
        return this.y;
    }

    public void startBgm(String var1, boolean var2) {
        this.M = var1;
        if(TextUtils.isEmpty(var1)) {
            this.s.stop();
        } else {
            this.s.start(var1, var2);
        }

        this.B.setBgmMusicPath(var1);
    }

    public void stopBgm() {
        this.s.stop();
        this.B.setBgmMusicPath((String)null);
    }

    public void setBGMRanges(long var1, long var3, boolean var5) {
        if(var1 >= 0L && var3 >= var1) {
            this.s.setPlayableRanges(var1, var3);
            this.B.setBGMRanges(var1, var3);
            if(var5) {
                this.s.restart();
            }
        }

    }

    public void seekBGM(long var1) {
        if(this.s.getMediaPlayer() != null && this.p.a().isPlaying()) {
            PlayRanges var3 = this.s.getPlayableRanges();
            if(var3 != null) {
                if(var1 < var3.startTime) {
                    var1 = var3.startTime;
                }

                if(var1 > var3.endTime) {
                    var1 = var3.endTime;
                }
            }

            if(var1 > this.x) {
                var1 = this.x;
            }

            if(var1 < 0L) {
                var1 = 0L;
            }

            this.s.getMediaPlayer().seekTo(var1);
        }

    }

    public void setBgmVolume(float var1) {
        this.s.setVolume(var1);
        this.B.setAudioVolume(this.B.mIdxAudioBgm, var1);
    }

    public float getBgmVolume() {
        return this.s.getVolume();
    }

    public void setLooping(boolean var1) {
        if(this.p != null) {
            this.p.c(var1);
        }

    }

    public void pausePlay(boolean var1) {
        if(var1) {
            this.p.a().pause();
            this.s.getMediaPlayer().pause();
            this.t.pause();
        } else {
            this.p.a().start();
            this.s.getMediaPlayer().start();
            this.t.resume();
        }

        this.q.setKeepFrameOnResume(var1);
    }

    public KSYMediaPlayer getMediaPlayer() {
        return this.p.a();
    }

    public void showWaterMarkLogo(String var1, float var2, float var3, float var4, float var5, float var6) {
        if(this.N == null) {
            this.N = new MyKSYEditKit.b();
        }

        this.N.a = var1;
        this.N.b = var2;
        this.N.c = var3;
        this.N.d = var4;
        this.N.e = var5;
        this.N.f = var6;
        var6 = Math.max(0.0F, var6);
        var6 = Math.min(var6, 1.0F);
        float var7 = var2;
        float var8 = var3;
        if(this.j != 0 && this.h != 0) {
            float var9 = (float)this.j / (float)this.k;
            float var10 = (float)this.h / (float)this.i;
            float var11;
            if(var9 < var10) {
                var11 = (1.0F - var9 / var10) / 2.0F;
                var7 = var2 * var9 / var10 + var11;
            } else if(var9 > var10) {
                var11 = (1.0F - var10 / var9) / 2.0F;
                var8 = var3 * var10 / var9 + var11;
            }
        }

        this.m.showLogo(this.context, var1, var4, var5);
        if(this.B.getScaleMode() == 0) {
            this.n.setRenderRect(this.f, var7, var8, var4, var5, var6);
            this.B.showWaterMarkLogo(var1, var2, var3, var4, var5, var6);
        } else if(this.B.getScaleMode() == 1) {
            this.n.setRenderRect(this.f, var7, var8, var4, var5, var6);
            this.B.showWaterMarkLogo(var1, var7, var8, var4, var5, var6);
        } else {
            this.n.setRenderRect(this.f, var2, var3, var4, var5, var6);
            this.B.showWaterMarkLogo(var1, var2, var3, var4, var5, var6);
        }

    }

    public void addStickerView(KSYStickerView var1) {
        this.I = var1;
        if(this.J == null) {
            this.K = new TimerTask() {
                public void run() {
                    if(MyKSYEditKit.this.p.a().isPlaying()) {
                        boolean var1 = MyKSYEditKit.this.I.updateStickerDraw(MyKSYEditKit.this.p.a().getCurrentPosition());
                        if(var1) {
                            Message var2 = MyKSYEditKit.this.u.obtainMessage(2);
                            MyKSYEditKit.this.u.sendMessage(var2);
                        }
                    }

                }
            };
            this.J = new Timer();
            this.J.schedule(this.K, 10L, 100L);
        }

    }

    public void updateStickerDraw() {
        if(this.I != null) {
            boolean var1 = this.I.updateStickerDraw(this.p.a().getCurrentPosition());
            if(var1) {
                Message var2 = this.u.obtainMessage(2);
                this.u.sendMessage(var2);
            }
        }

    }

    public void addPaintView(View var1) {
        this.L = var1;
    }

    public void seekTo(long var1) {
        if(var1 > this.x) {
            var1 = this.x;
        }

        if(var1 < 0L) {
            var1 = 0L;
        }

        if(this.p.a() != null) {
            this.p.a().seekTo(var1);
        }

    }

    public void hideWaterMarkLogo() {
        this.N = null;
        this.m.hideLogo();
        this.B.hideWaterMarkLogo();
    }

    public static String getVersion() {
        return "1.5.3";
    }

    public void release() {
        if(this.u != null) {
            this.u.removeCallbacksAndMessages((Object)null);
            this.u = null;
        }

        if(this.J != null) {
            this.J.cancel();
            this.J = null;
        }

        if(this.I != null) {
            this.I.release();
        }

        this.B.setOnInfoListener((com.ksyun.media.shortvideo.kit.a.b)null);
        this.B.setOnErrorListener((com.ksyun.media.shortvideo.kit.a.a)null);
        this.B.release();
        this.m.release();
        this.s.release();
        this.p.a((OnPreparedListener)null);
        this.p.a((com.ksyun.media.player.IMediaPlayer.OnInfoListener)null);
        this.p.a((OnCompletionListener)null);
        this.p.a((com.ksyun.media.player.IMediaPlayer.OnErrorListener)null);
        this.p.g();
        this.l.release();
    }

    private void f() {
        this.B.release();
        this.B = new com.ksyun.media.shortvideo.kit.a(this.context);
        this.B.setOnErrorListener(this.W);
        this.B.setOnInfoListener(this.V);
    }

    private void g() {
        if((long)this.B.getPlayRanges() < this.x) {
            this.Q.s = 1;
        } else {
            this.Q.s = 0;
        }

        if(!TextUtils.isEmpty(this.M) && this.B.getBGMRanges() != null && this.B.getBGMRanges().endTime - this.B.getBGMRanges().startTime < this.s.getFileDuration()) {
            this.Q.t = 1;
        } else {
            this.Q.t = 0;
        }

        this.Q.a = this.R.a(this.B.getVideoBitrate());
        this.Q.b = this.R.a(this.B.getAudioBitrate());
        this.Q.d = this.B.getVideoFps();
        this.Q.p = this.R.d(this.B.getVideoEncodeMethod(), this.B.getVideoEncodeProfile());
        this.Q.e = this.R.b(this.B.getVideoEncodeMethod(), this.B.getVideoCodecId());
        this.Q.v = this.p.b();
    }

    private void a(boolean var1, int var2) {
        this.Q.c = this.R.a(this.B.getTargetWidth(), this.B.getTargetHeight());
        this.P = System.currentTimeMillis() - this.O;
        long var3 = this.x;
        if(this.B.getPlayRanges() > 0) {
            var3 = (long)this.B.getPlayRanges();
        }

        this.R.a(var1, var2, var3, this.P, this.Q);
    }

    public void setTitleUrl(String var1) {
        this.S = var1;
    }

    public void setTailUrl(String var1) {
        this.T = var1;
    }

    private void a(String var1) {
        HashMap var2 = new HashMap();
        LinkedList var3 = new LinkedList();
        var3.add(Uri.parse("file:" + var1));
        var2.put(Uri.parse("file:" + var1), new Boolean(false));
        File var4;
        if(!TextUtils.isEmpty(this.S)) {
            var4 = new File(this.S);
            if(var4.exists()) {
                var3.add(0, Uri.parse("file:" + this.S));
                var2.put(Uri.parse("file:" + this.S), new Boolean(true));
            }
        }

        if(!TextUtils.isEmpty(this.T)) {
            var4 = new File(this.T);
            if(var4.exists()) {
                var3.add(Uri.parse("file:" + this.T));
                var2.put(Uri.parse("file:" + this.T), new Boolean(true));
            }
        }

        AudioEncodeFormat var6 = (AudioEncodeFormat)this.B.getAudioEncodeFormat();
        final MyKSYMergeKit var5 = new MyKSYMergeKit(this.context);
        var5.setEncodeMethod(this.B.getVideoEncodeMethod());
        var5.setTargetSize(this.B.getTargetWidth(), this.B.getTargetHeight());
        var5.setVideoBitrate(this.B.getVideoBitrate());
        var5.setAudioBitrate(this.B.getAudioBitrate());
        var5.setVideoFps(this.B.getVideoFps());
        var5.setToTranscodeFiles(var2);
        var5.setAudioChannels(var6.getChannels());
        var5.setAudioSampleRate(var6.getSampleRate());
        var5.setVideoCodecId(this.B.getVideoCodecId());
        var5.setVideoProfile(this.B.getVideoEncodeProfile());
        var5.setIFrameInterval(this.B.getIFrameInterval());
        var5.setOnInfoListener(new MyKSYMergeKit.OnInfoListener() {
            public void onInfo(int var1, String var2) {
                switch(var1) {
                case 2:
                    MyKSYEditKit.this.a(4, var2);
                    break;
                case 3:
                    if(MyKSYEditKit.this.S != null && var5.getCurrentTransFileId() == 0) {
                        MyKSYEditKit.this.a(7, var2);
                    } else if(MyKSYEditKit.this.S == null && var5.getCurrentTransFileId() == 1 || var5.getCurrentTransFileId() == 2) {
                        MyKSYEditKit.this.a(8, var2);
                    }
                    break;
                case 100:
                    MyKSYEditKit.this.a(3, var2);
                    if(MyKSYEditKit.this.D != null) {
                        File var3 = new File(MyKSYEditKit.this.D);
                        if(var3.exists()) {
                            var3.delete();
                        }
                    }
                }

            }
        });
        var5.setOnErrorListener(new MyKSYMergeKit.OnErrorListener() {
            public void onError(int var1, int var2, long var3) {
                switch(var1) {
                case -100:
                case -1:
                    MyKSYEditKit.this.a(-4005, (long)var1);
                default:
                }
            }
        });
        var5.start(var3, this.C, (String)null, false);
    }

    public String getTmpComposePath(String var1) {
        String var2 = null;
        int var3 = var1.lastIndexOf("/");
        if(var3 >= 0) {
            var2 = var1.substring(0, var3);
        }

        if(var2 != null) {
            File var4 = new File(var2);
            if(!var4.exists()) {
                var4.mkdir();
            }
        }

        StringBuilder var5 = (new StringBuilder(var2)).append("/").append(System.currentTimeMillis());
        var5.append(".mp4");
        return var5.toString();
    }

    private class b {
        String a;
        float b;
        float c;
        float d;
        float e;
        float f;

        private b() {
        }
    }

    private static class a extends Handler {
        private final WeakReference<MyKSYEditKit> a;

        a(MyKSYEditKit var1, Looper var2) {
            super(var2);
            this.a = new WeakReference(var1);
        }

        public void handleMessage(Message var1) {
            MyKSYEditKit var2 = (MyKSYEditKit)this.a.get();
            switch(var1.what) {
            case 2:
                var2.I.draw();
            default:
            }
        }
    }

    public interface OnErrorListener {
        void onError(int var1, long var2);
    }

    public interface OnInfoListener {
        Object onInfo(int var1, String... var2);
    }
}
